package com.ruoyi.quartz.task;


import com.alibaba.fastjson2.JSON;
import com.ruoyi.quartz.pojo.BandwidthSpeed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import oshi.SystemInfo;
import oshi.hardware.NetworkIF;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Component
public class VpnTask {
    @Autowired
    RedisTemplate<String,String> redisTemplate;
    /**
     * 添加系统当前网速
     */
    @Scheduled(fixedRate = 1000)
     public void updateBandwidthSpeed() throws InterruptedException {
        SystemInfo systemInfo = new SystemInfo();
        List<NetworkIF> networkIFs = systemInfo.getHardware().getNetworkIFs();
        // 存储上一次的发送/接收字节数
        Map<String, Long> prevRecvMap = new HashMap<>();
        Map<String, Long> prevSentMap = new HashMap<>();
        // 第一次采样
        for (NetworkIF net : networkIFs) {
            net.updateAttributes();
            prevRecvMap.put(net.getName(), net.getBytesRecv());
            prevSentMap.put(net.getName(), net.getBytesSent());
        }
        // 等待1秒
        Thread.sleep(1000);

        // 第二次采样并计算速率
        for (NetworkIF net : networkIFs) {
            net.updateAttributes();
            String name = net.getName();
            // 只处理物理网卡
            if (!name.startsWith("eth") && !name.startsWith("wlan") && !name.startsWith("en") && !name.startsWith("wl")) {
                continue;
            }
            long prevRecv = prevRecvMap.getOrDefault(name, 0L);
            long prevSent = prevSentMap.getOrDefault(name, 0L);

            long recvSpeed = net.getBytesRecv() - prevRecv;
            long sentSpeed = net.getBytesSent() - prevSent;

            // 防止负值
            recvSpeed = Math.max(recvSpeed, 0);
            sentSpeed = Math.max(sentSpeed, 0);

            //当前下载网速
            BigDecimal currRecvSpeed =  new BigDecimal(Long.toString(recvSpeed)).divide(new BigDecimal("1024"),2,BigDecimal.ROUND_HALF_UP);
            //当前上传网速
            BigDecimal currSentSpeed =  new BigDecimal(Long.toString(sentSpeed)).divide(new BigDecimal("1024"),2,BigDecimal.ROUND_HALF_UP);


//            System.out.printf("网卡: %s\n", name);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String timeString = formatter.format(LocalDateTime.now());
            BandwidthSpeed bandwidthSpeed = new BandwidthSpeed();
            bandwidthSpeed.setDownload(currRecvSpeed);
            bandwidthSpeed.setUpload(currSentSpeed);
            bandwidthSpeed.setDate(timeString);
            //将当前秒加载到list最右边
            redisTemplate.opsForList().leftPushAll("bandwidth:system:speed", JSON.toJSONString(bandwidthSpeed));
            Long size = redisTemplate.opsForList().size("bandwidth:system:speed");

            if (size>=31){
                redisTemplate.opsForList().trim("bandwidth:system:speed",0,29);
            }
        }
     }
}
